package com.fyzk.activity.lottery.jc.football.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.BaseExpandableListAdapter;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fyzk.activity.app.App;
import com.fyzk.activity.app.AppToast;
import com.fyzk.activity.lottery.jc.football.JCInfo;
import com.fyzk.activity.lottery.jc.football.OnJCZQAdapterClick;
import com.fyzk.activity.lottery.jc.football.newJz.data.NewScoreDetailActivity;
import com.fyzk.bean.JCZQResponseInfo;
import com.fyzk.net.PublicMethod;
import com.fyzk.utils.AppUtil;
import com.tyjc.lottery.R;

import java.text.SimpleDateFormat;
import java.util.List;

import http.DataUtil.text.TextUtil;

public class JCZQ_BF_Adapter extends BaseExpandableListAdapter {

    private final int maxCount;
    protected OnJCZQAdapterClick listener;
    private List<JCZQResponseInfo> infos;
    protected Context context;
    private int count;

    public JCZQ_BF_Adapter(Context context,
                           OnJCZQAdapterClick listener, List<JCZQResponseInfo> infos) {
        this.infos = infos;
        this.context = context;
        this.listener = listener;
        if (App.isBaskOrder) {
            maxCount = 8;
        } else {
            maxCount = 10;
        }
    }

    public void setCount(int count) {
        this.count = count;
    }

    public void setInfos(List<JCZQResponseInfo> infos, int type) {
        switch (type) {
            case 0:
//			 this.infos.clear();
//			 ArrayList<JCInfo> copyInfos = new ArrayList<JCInfo>(infos);
//			 for (JCInfo info : copyInfos) {
//			 if (info.getSupport().contains(lotteryId+"")) {
//			 this.infos.add(info);
//			 }
//			 }
                this.notifyDataSetChanged();
                break;
            case 1:

                break;
            case 2:

                break;
            default:
                break;
        }

    }

    @Override
    public int getGroupCount() {
        return infos.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return infos.get(groupPosition).matchDatas.size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return infos.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return infos.get(groupPosition).matchDatas.get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return groupPosition * 100 + childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup
            parent) {
        JCZQResponseInfo info = infos.get(groupPosition);
        GroupHolder groupHolder = null;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.jclq_adapter_group, null);
            groupHolder = new GroupHolder();
            groupHolder.date_tv = (TextView) convertView
                    .findViewById(R.id.jclq_group_tv);
            groupHolder.jianTou_iv = (ImageView) convertView
                    .findViewById(R.id.jclq_group_iv);
            convertView.setTag(groupHolder);
        } else {
            groupHolder = (GroupHolder) convertView.getTag();
        }

        groupHolder.date_tv.setText(info.matchTime + "   " + info.matchWeekday
                + "    共 " + info.matchDatas.size() + " 场");
        if (isExpanded) {
            groupHolder.jianTou_iv.startAnimation(AnimationUtils.loadAnimation(
                    context, R.anim.title_jiantou_shun));
        } else {
            groupHolder.jianTou_iv.startAnimation(AnimationUtils.loadAnimation(
                    context, R.anim.title_jiantou_ni));
        }

        return convertView;
    }


    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean
            isLastChild, View
                                     convertView, ViewGroup parent) {
        JCInfo infor = infos.get(groupPosition).matchDatas.get(childPosition);
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = View.inflate(context, R.layout.zcbf_list, null);
            holder.ll_xinxi = (LinearLayout) convertView
                    .findViewById(R.id.zcbf_list_left);
            holder.ll_xiangqing = (LinearLayout) convertView
                    .findViewById(R.id.zcbf_xiangqing);
            holder.ll_right = (LinearLayout) convertView
                    .findViewById(R.id.jczq_list_right);
            holder.ivIsDanGuan = (ImageView) convertView.findViewById(R.id.ivIsDanGuan);
            holder.left_tv1 = (TextView) convertView
                    .findViewById(R.id.zcbf_list_teamname);
            holder.left_tv2 = (TextView) convertView
                    .findViewById(R.id.zcbf_list_teamnum);
            holder.left_tv3 = (TextView) convertView
                    .findViewById(R.id.zcbf_list_endtime);
            holder.zcbf_grid = (GridView) convertView
                    .findViewById(R.id.zcbf_grid);
            holder.defult = (TextView) convertView.findViewById(R.id.jczq_list_default);
            holder.fl_open = (FrameLayout) convertView.findViewById(R.id.jczq_list_zhankai);
            holder.right_tv1 = (TextView) convertView.findViewById(R.id.jczq_list_right_tv1);
            holder.right_tv2 = (TextView) convertView.findViewById(R.id.jczq_list_right_tv2);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }


        holder.right_tv1.setText(infor.getHomeTeam());
        holder.right_tv2.setText(infor.getAwayTeam());

        holder.fl_open.setTag(R.string.jc_info, infor);
        holder.fl_open.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                JCInfo infor = (JCInfo) v.getTag(R.string.jc_info);
                if (infor.getOnclikNum() == 0 && count == maxCount) {
                    AppToast.getToast().show("最多选择" + maxCount + "场比赛");
                } else {
                    if (listener != null) {
                        listener.onClick_ZhanKai(groupPosition, childPosition);
                    }
                }
            }
        });

        // 事件
        holder.left_tv1.setBackgroundResource(AppUtil.getTeamNameColor(infor.getMatchName()));
        holder.left_tv1.setText(infor.getMatchName());

        holder.left_tv2.setText(PublicMethod.getTeamNum(infor.getMatchNum()));
        SimpleDateFormat format = new SimpleDateFormat("HH:mm");
        holder.left_tv3.setText(format.format(infor.getEndSaleTime()) + "[截]");


        if (infor.isShowHideView) {
            holder.ll_xiangqing.setVisibility(View.VISIBLE);
        } else {
            holder.ll_xiangqing.setVisibility(View.GONE);
        }
        if (infor.hideView != null) {
            ViewGroup parent1 = (ViewGroup) infor.hideView.getParent();
            if (parent1 != null) {
                parent1.removeAllViews();
            }
            holder.ll_xiangqing.removeAllViews();
            holder.ll_xiangqing.addView(infor.hideView);
        }

        holder.ll_xinxi.setTag(R.string.jc_info, infor);
        holder.ll_xinxi.setTag(holder.ll_xiangqing);
        // 点击显示隐藏信息
        holder.ll_xinxi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                JCInfo infor = (JCInfo) v.getTag(R.string.jc_info);
//                LinearLayout layout = (LinearLayout) v.getTag();
//                if (infor.hideView == null) {
//                    infor.hideView = new FootballMatchSummaryView(context, infor.getMatchNum());
//                    layout.removeAllViews();
//                    layout.addView(infor.hideView);
//                }
//                infor.isShowHideView = !infor.isShowHideView;
//                if (infor.isShowHideView) {
//                    layout.setVisibility(View.VISIBLE);
//                } else {
//                    layout.setVisibility(View.GONE);
//                }

                Intent intent = new Intent(context, NewScoreDetailActivity.class);
                intent.putExtra("isFootball", true);
                intent.putExtra("matchNum", infor.getMatchNum());
                context.startActivity(intent);
            }

        });
        holder.ll_xiangqing.setTag(R.string.jc_info, infor);
//        holder.ll_xiangqing.setOnClickListener(new View.OnClickListener() {
//
//            @Override
//            public void onClick(View v) {
//                // onClick1(JCZQ_BF_Activity.WHAT_FENXI, position);
//                JCInfo infor = (JCInfo) v.getTag(R.string.jc_info);
//                Intent intent = new Intent(context, ScoreDetailActivity.class);
//                intent.putExtra("matchNum", infor.getMatchNum());
//                context.startActivity(intent);
//            }
//        });

        if (TextUtil.isEmpty(infor.getResult())) {
            holder.defult.setVisibility(View.VISIBLE);
            holder.defult.setText("");
            holder.zcbf_grid.setVisibility(View.GONE);
        } else {
            holder.defult.setVisibility(View.GONE);
//            holder.defult.setText(infor.getResult());
            holder.zcbf_grid.setVisibility(View.VISIBLE);
//            BFGridAdapter bfGridAdapter = (BFGridAdapter) holder.zcbf_grid.getAdapter();
//            if (bfGridAdapter == null) {
            BFGridAdapter bfGridAdapter = new BFGridAdapter(infor, context);
            holder.zcbf_grid.setAdapter(bfGridAdapter);
//            } else {
//                LogUtil.v("childPosition>"+childPosition+"|setData>>>>>");
//                bfGridAdapter.setData(infor);
//            }
//            if(listener!=null){
//                listener.onDataChanged();
//                bfGridAdapter.notifyDataSetChanged();
//            }
        }

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

    class GroupHolder {
        TextView date_tv;
        ImageView jianTou_iv;
    }

    class ViewHolder {
        LinearLayout ll_xinxi;
        LinearLayout ll_xiangqing;
        ImageView ivIsDanGuan;
        FrameLayout fl_open;
        LinearLayout ll_right;
        GridView zcbf_grid;
        TextView left_tv1;
        TextView left_tv2;
        TextView left_tv3;
        TextView defult;
        TextView right_tv1;
        TextView right_tv2;
    }

}
